package module.main.model
{
	import com.darwin.darwinMVC.core.CmdNameList;
	import com.darwin.darwinMVC.mvc.Command;
	import com.darwin.darwinNet.Net;
	
	import main.AppConfig;
	import main.mvc.GModelData;
	
	import module.main.cmds.MainCmdConst;
	
	/**
	 *	好友数据模块 
	 * 	@author killsoon
	 */	
	public class FriendInfoModelData extends GModelData
	{
		public function FriendInfoModelData()
		{
//			Net.getInstance().addEventListener(
				
			var cmd:CmdNameList = new CmdNameList(MainCmdConst.GET_FRIEND_LIST,MainCmdConst.GET_FRIEND_GAME_DATA);
			register(cmd,friendInfo);
			//			Net.getInstance().registerModelHandle(MainModelDataConst.GET_USER_INFO,userInfo);
		}
		
		/**
		 *	处理后台返回好友信息 
		 * 	@param data	
		 */		
		private function friendInfo(data:Object):void
		{
			var friend_list:Array = [];
			var friend_game_data:Object = {};
			
			if(data.hasOwnProperty("friend_list"))
			{
				friend_list = data.friend_list as Array;
			}
			if(data.hasOwnProperty("friend_game_data"))
			{
				friend_game_data = data.friend_game_data;
			}
			
			if(friend_list && friend_game_data)
			{
				bindingFriendData(friend_list, friend_game_data);
			}
		}
		
		/**
		 *	捆绑好友信息
		 * （把friend_game_data中的level字段添加到friend_list中） 
		 */		
		private function bindingFriendData(friend_list:Array, friend_game_data:Object):void
		{
			var length:int = friend_list.length;
			
			for(var i:int = 0; i < length; i++)
			{
				if(friend_game_data.hasOwnProperty(friend_list[i].fm_uid))
				{
					friend_list[i].grade = friend_game_data[friend_list[i].fm_uid].level;
				}
				else
				{
					friend_list[i].grade = 300;
				}
			}
			sortFriendByGrade(friend_list);
		}
		
		/**
		 *	按好友等级从高到底排序 
		 */	
		private function sortFriendByGrade(friend_list:Array):void
		{
			friend_list.sortOn("grade", Array.NUMERIC | Array.DESCENDING);
			var cmd:Command = new Command(MainCmdConst.MAIN_CMD_FRIENDINFO, this);
			cmd.data = friend_list;
			dispatch(cmd);
		}
	}
}